# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

#
# usage: python 5b_get-normalized-noun-statistics.py <noun-statistics-file>
# 
# get the normalized yearly statistics for a given noun statistics file

import sys

# load the yearly stats
fd1 = open("yearly-stats.txt", "r")

line = fd1.readline().strip()
record = {}

while line:
	year, count = line.split()
	record[year] = int(count) 
	line = fd1.readline().strip()

fd1.close()

# open the non normalized noun stats
fd2 = open(sys.argv[1], "r")

line2 = fd2.readline().strip()

record2 = {}

# read the stats file
while line2:
        ngram, year, matchCount, pageCount, volumeCount = line2.split(',')
	if (int(year) >= 1800 and int(year) <= 2000):
        	yearlyCount = record[year]
		record2[year] = float(float(matchCount) / float(record[year]))
	line2 = fd2.readline().strip()

fd2.close()

# output file
fd3 = open("normalized-" + sys.argv[1], "w")

for key in sorted(record2.iterkeys()):
	fd3.write(key + " " + str(record2[key]) + "\n")

fd3.close()
